<?php

// +------------------------------------------------------------
// | Name: 星空框架 产品模型
// +------------------------------------------------------------
// | Version: V5.0
// +------------------------------------------------------------
// | Author: xkyinzhe <zhonghuaxinxing@sina.cn>
// +------------------------------------------------------------
// | Time: 2021-04-25 14:36
// +------------------------------------------------------------
declare(strict_types=1);

namespace app\common\model;

use think\model\concern\SoftDelete;
use app\common\model\SpecPrice as SpecPriceModel;
use app\common\model\GoodsParameters as GoodsParametersModel;
use app\common\model\GoodsPic as GoodsPicModel;
use app\common\model\Car as CarModel;

/**
 * @xk 产品模型管理
 */
class Goods extends BaseModel
{
    use SoftDelete;
    protected $name = 'goods';
    protected $pk = 'goods_id';
    protected $deleteTime = 'delete_time';
    /**
     * @xk 写入前操作
     *
     * @method onBeforeWrite
     * @param  [type]                  $data                     [description]
     * @return [type]                                            [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-27T09:17:57+080
     */
    public static function onBeforeWrite($data)
    {
        unset($data['act']);
        unset($data['controllers']);
        unset($data['actions']);
        if (!$data->create_time) {
            $data->update_time = $data->create_time = date('Y-m-d H:i:s');
        }
        if ($data->order_id <= 0) {
            $data->order_id = self::max('order_id')+1;
        }
    }
    /**
     * @xk 删除后操作
     *
     * @method onAfterDelete
     * @param  [type]                  $data                     [description]
     * @return [type]                                            [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-27T10:26:28+080
     */
    public static function onAfterDelete($data)
    {
        $specPriceModel = new SpecPriceModel();
        $goodsParametersModel = new GoodsParametersModel();
        $goodsPicModel = new GoodsPicModel();
        $carModel = new CarModel();
        $goods_id = $data['goods_id'];
        $condition = [];
        $condition[] =['goods_id','=',$goods_id];
        // 删除商品规格价格体系
        $spec_res = $specPriceModel->destroy(function ($query) use ($condition) {
            $query->where($condition);
        });
        // 删除商品参数体系
        $goods_parameters_res = $goodsParametersModel->destroy(function ($query) use ($condition) {
            $query->where($condition);
        });
        // 删除商品购物车体系
        $goods_car_res = $carModel->destroy(function ($query) use ($condition) {
            $query->where($condition);
        });
        // 删除商品收藏体系
    }
    /**
     * @xk 获取商品货号
     *
     * @method getGoodsSn
     * @param  [type]                  $goods_sn                 [description]
     * @return [type]                                            [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-27T09:59:08+080
     */
    public static function getGoodsSn($goods_sn = '')
    {
        if ($goods_sn == '') {
            $goods_sn = self::getCodeNo(date('YmdHis'), 20, 1, 'goods_sn');
        }
        return $goods_sn;
    }
    /**
     * @xk 获取产品参数
     *
     * @method getGoodsParameters
     * @return [type]                  [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-27T10:53:52+080
     */
    public function getGoodsParameters()
    {
        return $this->hasMany('goodsParameters', 'goods_id', 'goods_id');
    }
    /**
     * @xk 获取产品组图
     *
     * @method getPic
     * @return [type]                  [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-05-07T11:15:34+080
     */
    public function getPic()
    {
        return $this->hasMany('goodsPic', 'goods_id', 'goods_id');
    }
}
